package com.ikas.ai.framework.node.impl;

import com.ikas.ai.framework.core.model.NodeInputVariables;
import com.ikas.ai.framework.flow.ScoreFlow;
import com.ikas.ai.framework.node.NodeType;

import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Description: 组合判断
 * @Author: qi.yilai
 * @Date: 2023/10/31 19:24
 * @Version: 1.0.0
 **/
public class CompositePredictNode extends AbstractNode<Boolean> {

    private String operator;
    private int number;

    public CompositePredictNode(ScoreFlow scoreFlow, String operator, int number, Long nodeId, Long nodeInstanceId, String name) {
        super(scoreFlow, nodeId, nodeInstanceId, name);
        this.operator = operator;
        this.number = number;
    }

    @Override
    public NodeType type() {
        return NodeType.COMPOSITE_PREDICT;
    }

    @Override
    public NodeInputVariables outVariables() {
        NodeInputVariables nodeInputVariables = NodeInputVariables.copy(previous);
        nodeInputVariables.clearPredicts();
        mergeData(nodeInputVariables, result);
        return nodeInputVariables;
    }

    @Override
    Boolean doExecute() {
        List<Boolean> list = previous().getPredicts();
        long num = list.stream().filter(t -> t).count();
        if (">".equals(operator)) {
            return num > number;
        } else if (">=".equals(operator)) {
            return num >= number;
        } else if ("<".equals(operator)) {
            return num < number;
        } else if ("<=".equals(operator)) {
            return num <= number;
        } else if ("==".equals(operator)) {
            return num == number;
        } else {
            throw new UnsupportedOperationException("条件组合判断只支持运算符 <, <=, >, >=, ==");
        }
    }
}
